let app = getApp();
import { getCar } from "../../../../request/api.js";
import dayjs from "../../../../common/js/dayjs.js";

Page({

    /**
     * 页面的初始数据
     */
    data: {
        lang: null,
        carBox: [],
        filterType: null,
        filterBrandType: null,

        // 分页
        pageNo: 1,
        totalPage: 0,
    },

     // 首页
     homePage() {
        if(this.data.filterType > 0) {
            let status = 1
            let data = { status, type: this.data.filterType, pageNo: this.data.pageNo }
            getCar(res => {
                let request = res.data.data
                if (res.statusCode == 200) {
                    for (let item of res.data.data) {
                        item.releaseTime = dayjs(item.releaseTime).format("YYYY-MM-DD")
                    }
                    this.setData({ carBox: this.data.carBox.concat(request), totalPage : res.data.totalPage })
                }
            }, data);
        }

        if (this.data.filterBrandType > 0) {
            let status = 1
            let data = { status, brand_type: this.data.filterBrandType, pageNo: this.data.pageNo }
            getCar(res => {
                let request = res.data.data
                if (res.statusCode == 200) {
                    for (let item of res.data.data) {
                        item.releaseTime = dayjs(item.releaseTime).format("YYYY-MM-DD")
                    }
                    this.setData({ carBox: this.data.carBox.concat(request), totalPage: res.data.totalPage })
                }
            }, data);
        }
    },

    // 跳页到详细页面
    goDetailed(event) {
        let itemId = event.currentTarget.dataset.item.id
        wx.navigateTo({
            url: '/pages/home/car/carDetailed/carDetailed?itemId=' + itemId
        });
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        this.setData({ lang: app.globalData.lang, navTitle: "carInfo", navigation: true, filterType: options.filterType ? options.filterType : 0, filterBrandType: options.filterBrandType ? options.filterBrandType : 0 })
        this.homePage()
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {
        if(this.data.pageNo == this.data.totalPage) return
        let pageNo = this.data.pageNo += 1
        this.setData({pageNo : pageNo})
        this.homePage();
    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})